﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Core.Entities;
using Library.Extensions;

namespace Core.Queries
{
    public class ItemValueQuery : BaseQuery<ItemValue>
    {        
        public int[] ItemIDs { get; set; }
        public int? ArticleID { get; set; }        
        public int ColumnID { get; set; }
        public string Value { get; set; }

        public override Expression<Func<ItemValue, bool>> ToExpression()
        {            
            Expression<Func<ItemValue, bool>> predicate = x => true;     

            if (ArticleID.HasValue)
                predicate = x => x.Column.ArticleID == ArticleID;

            if (!string.IsNullOrWhiteSpace(Value))
                predicate = x => x.ColumnID == ColumnID && x.Value.StartsWith(Value);

            if (ItemIDs != null && ItemIDs.Length > 0)
                predicate = x => ItemIDs.Contains(x.ItemID);

            return predicate;
        }
    }
}
